/*
 * MoveElementsToScenarioAction.java
 * 
 * Created by demory on Jul 26, 2008, 9:57:11 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.apps.rtemaster.actions;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.fpdev.core.Scenario;
import org.fpdev.core.basenet.BLink;
import org.fpdev.core.basenet.BNode;
import org.fpdev.core.data.SpatialDataConnection.LinkAttr;
import org.fpdev.core.data.SpatialDataConnection.NodeAttr;
import org.fpdev.apps.rtemaster.RouteMaster;

/**
 *
 * @author demory
 */
public class MoveElementsToScenarioAction implements ACAction {

  private Set<BNode> nodes_;
  private Set<BLink> links_;

  private Map<Integer, Scenario> oldNodeScenarios_;
  private Map<Integer, Scenario> oldLinkScenarios_;
  
  private Scenario scen_;
  
  public MoveElementsToScenarioAction(Set<BNode> selNodes, Set<BLink> selLinks, Scenario scen) {
    nodes_ = selNodes;
    links_ = selLinks;
    scen_ = scen;
    
    oldNodeScenarios_ = new HashMap<Integer, Scenario>();
    oldLinkScenarios_ = new HashMap<Integer, Scenario>();
    
  }
  
  public boolean doAction(RouteMaster av) {
    for (BNode node : nodes_) {
      oldNodeScenarios_.put(node.getID(), node.getScenario());
      node.setScenario(scen_);
    }
    for (BLink link : links_) {
      oldLinkScenarios_.put(link.getID(), link.getScenario());
      link.setScenario(scen_);
    }

    av.getEngine().getDataPackage().getSpatialDataConn().modifyNodes(nodes_, new NodeAttr[] { NodeAttr.SCEN });
    av.getEngine().getDataPackage().getSpatialDataConn().modifyLinks(links_, new LinkAttr[] { LinkAttr.SCEN });
    //av.getEngine().getDB().updateNodes(nodes_.iterator());
    //av.getEngine().getDB().updateLinks(links_.iterator());
  
    av.msg(nodes_.size()+" nodes and "+links_.size()+" links moved to scenario \""+scen_.getName()+"\"");
  
    return true;
  }

  public boolean undoAction(RouteMaster av) {
    for (Map.Entry<Integer, Scenario> entry : oldNodeScenarios_.entrySet())
      av.getEngine().getNodeFromID(entry.getKey()).setScenario(entry.getValue());
    for (Map.Entry<Integer, Scenario> entry : oldLinkScenarios_.entrySet())
      av.getEngine().getLinkFromID(entry.getKey()).setScenario(entry.getValue());
  
    av.getEngine().getDataPackage().getSpatialDataConn().modifyNodes(nodes_, new NodeAttr[] { NodeAttr.SCEN });
    av.getEngine().getDataPackage().getSpatialDataConn().modifyLinks(links_, new LinkAttr[] { LinkAttr.SCEN });
    //av.getEngine().getDB().updateNodes(nodes_.iterator());
    //av.getEngine().getDB().updateLinks(links_.iterator());

    av.msg(nodes_.size()+" nodes and "+links_.size()+" links restored to original scenario(s)");
    return true;
  }

  public String getName() {
    return "Move Selected Links to Scenario";
  }

}
